hw1

參數的傳值、傳參考(傳遞一個C++物件)

http://openhome.cc/Gossip/CppGossip/PassBy.html

#include <iostream>
using namespace std;

class Box
{

public:
    Box()
    {
        length = 0;
        breadth = 0;
        height = 0;
        cout << "Box constructor called";
    }

    Box(double L, double B, double H)
    {
        length = L;
        breadth = B;
        height = H;
        cout << "Box constructor called" << endl;
    }

    double Volume()
    {
        return length*breadth*height;
    }

    bool CompareVolume(Box &CompareBox)
    {
        if (this->Volume() > CompareBox.Volume())
            return 1;
        else
            return 0;

    }

    void set_length(double L)
    {
        length = L;
    }
    void set_breadth(double B)
    {
        breadth = B;
    }
    void set_height(double H)
    {
        height = H;
    }

private:
    double length, breadth, height;

};

int main()
{

    Box firstBox(19.0, 11.0, 5.0);
    Box secondBox(17.0, 10.0, 8.0);

    cout << "Volume of first Box = " << firstBox.Volume() << endl;
    cout << "Volume of second Box = " << secondBox.Volume() << endl;


    cout << "The first Box is "
        << (firstBox.CompareVolume(secondBox) == 1 ? "" : "not ")
        << "greater than the second Box.";

    system("pause");
    return 0;

}

/*

public:
Box, Volume, CompareVolume

private:
length, breadth, height

Box firstBox(19.0, 11.0, 5.0);
Box secondBox(17.0, 10.0, 8.0);


*/

results matching ""

    No results matching ""